package com.pignest.hotel.ui.reception;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Spinner;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbGoods;
import com.pignest.hotel.beans.TbLiveGoodsBorrow;
import com.pignest.hotel.definition.GoodsType;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class BorrowProjectWindow extends Window implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8439584366084774725L;

	/**
	 * 选择借用项目如电风吹、衣架、充电器等
	 */


	private ReceptionService receptionService;
	
	public Living living;
	public TbGoods goods;
	private Listbox nameTextbox;

	private Textbox countField;//数量 
	private Textbox price1Textbox;//单价
	private Textbox residualTextbox;//剩余的数量
    private Label opTip;
	
	public BorrowProjectWindow() {
		receptionService = Services.getReceptionService();
		addEventListener("onCreate", this);
	}
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			nameTextbox = (Listbox) getFellow("nameField");

			//projectsListbox=(Listbox) getFellow("projectsListbox");
			countField=(Textbox)getFellow("countField");
			opTip=(Label)getFellow("opTip");
			price1Textbox=(Textbox)getFellow("price1box");
			residualTextbox=(Textbox)getFellow("residualbox");
			setGoodsList();
			queryProjects();
		}
	}

	public void query(){
		queryProjects();
		
	}
	/*
	 * 定位到只有一条记录
	 * **/
	public void queryProjects(){
		StringBuffer hql=new StringBuffer();
	    String name =UIUtils.getSelectedValue(nameTextbox);

	    hql.append(" from TbGoods where 1=1 and type='").append(GoodsType.OnBorrow.name()).append("'");
	    if(name!=null&&!name.equals(""))
	    	hql.append(" and id = '"+name+"'");
        List <TbGoods> atList=receptionService.find(hql.toString());

        if(atList!=null&&atList.size()!=0){
        	TbGoods s=atList.get(0);
        	this.goods=s;
        	price1Textbox.setValue(MoneyUtil.toString(s.getPrice()));
        	Integer c =s.getCount();
        	if(c!=null)
        	  residualTextbox.setValue(Long.toString(s.getCount()));
        	else
        	 residualTextbox.setValue("0");
        }
        	
	}
    public void setGoodsList(){
    	StringBuffer hql=new StringBuffer();

    	hql.append(" from TbGoods where 1=1 and type='").append(GoodsType.OnBorrow).append("'");

    	 List <TbGoods> list=receptionService.find(hql.toString());
    	 if(list==null)
    		 return ;
    	 for(TbGoods s:list){
    		 Listitem listitem = new Listitem();
 	    	listitem.setParent(nameTextbox);
 	    	listitem.setValue(s.getId());
 	    	listitem.appendChild(new Listcell(s.getName()));
    	 }
    }
	public Living getLiving() {
		return living;
	}
	public void setLiving(Living living) {
		this.living = living;
	}

    public void queryProjectListbox(){
    	nameTextbox.getItems().clear();
    	setGoodsList();
    }

    /**
     * 增加借用
     * 
     * ***/
	public void addBorrowProjeccts(){
		if(!Utils.checkPrice(countField.getValue())){
			opTip.setValue("请输入正确的数量");
			countField.focus();
			return ;
		}
		if(this.goods==null){
			opTip.setValue("请选择借用的物品!");
			return ;
		}
		if(this.living!=null){
			TbLiveGoodsBorrow gb = new TbLiveGoodsBorrow();
			gb.setLiving(living);
			gb.setTbGoods(this.goods);
			gb.setCount(Integer.valueOf(countField.getValue()));
			gb.setTime(new Date());      
            receptionService.insert(gb);
            opTip.setValue("操作成功！");

		}
	}
	
}
